import { dateFormat } from '@/utils/string'
import qs from 'qs'

let roleList = [
  {
    roleId: 'ADMIN',
    roleName: '超级管理员',
    isSys: 1,
    createTime: '2020-05-20',
    roleDesc: '系统的超级管理员'
  },
  {
    roleId: 'MANAGER',
    roleName: '管理员',
    isSys: 1,
    createTime: '2020-05-20',
    roleDesc: '系统的普通管理员'
  },
  {
    roleId: 'USER',
    roleName: '普通用户',
    isSys: 1,
    createTime: '2020-05-20',
    roleDesc: '普通用户，新建用户时会自动赋予的角色'
  },
  {
    roleId: 'TESTROLE',
    roleName: '测试角色',
    isSys: 0,
    createTime: '2020-05-20',
    roleDesc: '测试角色，非系统角色可物理删除'
  }
]

export default {
  'get|/sys/role/list': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const params = url.split('?')[url.split('?').length - 1]
    const data = qs.parse(params)
    const name = data.name
    const newList = []
    // mock模拟数据不做分页
    roleList.forEach((item) => {
      // 过滤-超级管理员
      if (item.roleId === 'ADMIN') {
        return true
      }
      if (name !== '') {
        if (item.roleName.indexOf(name) > -1 || item.roleId.indexOf(name) > -1) {
          newList.push(item)
          return true
        }
      } else {
        newList.push(item)
      }
    })
    ret.data = {
      totalCount: roleList.length - 1,
      dataList: newList
    }
    return ret
  },
  'get|/sys/role/info': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const roleId = url.split('/')[url.split('/').length - 1]
    const role = roleList.find(item => {
      return item.roleId === roleId
    })
    if (role) {
      const newRole = {
        roleId: role.roleId,
        roleName: role.roleName,
        roleDesc: role.roleDesc
      }
      ret.data = newRole
    } else {
      ret.msg = '无此id[' + roleId + ']的角色'
    }
    return ret
  },
  'delete|/sys/role/info': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const roleId = url.split('/')[url.split('/').length - 1]
    let index = roleList.findIndex((item) => {
      return item.roleId === roleId
    })
    if (index > -1) {
      roleList.splice(index, 1)
      ret.msg = '成功!请关闭mockjs开启后台体验完整功能！'
    } else {
      ret.code = 500
      ret.msg = '失败,未找到此角色!'
    }
    return ret
  },
  'post|/sys/role/info': option => {
    let ret = {code: 200, msg: '', data: null}
    let role = JSON.parse(option.body)
    const exist = roleList.some(item => {
      return item.roleId === role.roleId
    })
    if (!exist) {
      role['isSys'] = 0
      role['createTime'] = dateFormat(new Date(), 'YYYY-MM-DD')
      roleList.push(role)
      ret.msg = '成功！请关闭mockjs开启后台体验完整功能！'
    } else {
      ret.code = 500
      ret.msg = '失败，该角色名称已存在！'
    }
    return ret
  },
  'put|/sys/role/info': option => {
    let ret = {code: 200, msg: '', data: null}
    const role = JSON.parse(option.body)
    let isUpdate = roleList.some(item => {
      if (item.roleId === role.roleId) {
        item.roleName = role.roleName
        item.roleDesc = role.roleDesc
        return true
      }
    })
    if (isUpdate) {
      ret.msg = '成功！请关闭mockjs开启后台体验完整功能！'
    } else {
      ret.code = 500
      ret.msg = '失败,找不到此角色！'
    }
    return ret
  }
}
